package net.jxvtc.eshop.web.servlet.client;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.jxvtc.eshop.domain.Product;

/**
 * 璐墿杞﹀唴瀹瑰彉鏇�
 * @author admin
 *
 */
@WebServlet("/changeCart")
public class ChangeCartServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.寰楀埌鍟嗗搧id
		String id = request.getParameter("id");
		// 2.寰楀埌瑕佷慨鏀圭殑鏁伴噺
		int count = Integer.parseInt(request.getParameter("count"));
		// 3.浠巗ession涓幏鍙栬喘鐗╄溅.
		HttpSession session = request.getSession();
		Map<Product, Integer> cart = (Map<Product, Integer>) session.getAttribute("cart");
		Product p = new Product();
		p.setId(id);
		if (count != 0) {
			cart.put(p, count);
		} else {
			cart.remove(p);
		}
		response.sendRedirect(request.getContextPath() + "/client/cart.jsp");
		return;
	}
}
